package com.hzjh.base.style.view.loading;

import android.graphics.Bitmap;
import android.util.LruCache;

import java.lang.ref.SoftReference;

public class FrameAnimationCache extends LruCache<String, SizeSoftReferenceBitmap> {

    public static FrameAnimationCache cache = new FrameAnimationCache();
    public static FrameAnimationCache getInstance() {
        return cache;
    }

    public FrameAnimationCache() {
        super(150);
    }


    public Bitmap getBitmapFromCache(String key) {
        SizeSoftReferenceBitmap value = get(key);
        return value != null && value.getBitmap() != null ? value.getBitmap().get() : null;
    }

    public void addBitmapToCache(String key, Bitmap value) {
        put(key, new SizeSoftReferenceBitmap(new SoftReference<>(value), value.getByteCount()));
    }
}
class SizeSoftReferenceBitmap {
    private SoftReference<Bitmap> mBitmap;
    private int mSize;

    public SizeSoftReferenceBitmap(SoftReference<Bitmap> bitmap, int size) {
        mBitmap = bitmap;
        mSize = size;
    }

    public int getSize() {
        return mSize;
    }

    public SoftReference<Bitmap> getBitmap() {
        return mBitmap;
    }
}
